﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace BookStore.Data
{
    public class authorController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static authorInfo getbyID(int authorID)
        {
            authorInfo objAuthor = new authorInfo();
            using (SqlCommand cmd = new SqlCommand("getIDAuthor", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@authorID", authorID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objAuthor.authorID = dr.GetInt32(0);
                        objAuthor.authorName = dr.GetString(1).ToString();
                        objAuthor.address = dr.GetString(2).ToString();
                        objAuthor.phone = dr.GetString(3).ToString();
                    }
                }
                dr.Close();
            }
            return objAuthor;
        }

        #endregion

        #region getbyAll
        public static DataTable ListAuthor()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listAuthor", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertAuthor(authorInfo objAuthor)
        {
            using (SqlCommand cmd = new SqlCommand("insertAuthor", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@authorName", objAuthor.authorName));
                cmd.Parameters.Add(new SqlParameter("@address", objAuthor.address));
                cmd.Parameters.Add(new SqlParameter("@phone", objAuthor.phone));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateAuthor(authorInfo objAuthor)
        {
            using (SqlCommand cmd = new SqlCommand("updateAuthor", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@authorID", objAuthor.authorID));
                cmd.Parameters.Add(new SqlParameter("@authorName", objAuthor.authorName));
                cmd.Parameters.Add(new SqlParameter("@address", objAuthor.address));
                cmd.Parameters.Add(new SqlParameter("@phone", objAuthor.phone));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteAuthor(int authorID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteAuthor", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@authorID", authorID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region checkAuthor
        public static bool checkAuthor(int authorID) {
            SqlCommand cmd = new SqlCommand("checkAuthor", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@authorID", authorID));
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                return true;
            }
            dr.Close();
            return false;
        }
        #endregion
    }
}
